home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 June
/
Macworld (1999-06).dmg
/
Shareware World
/
Info
/
For Developers
/
MacZoop2.0.sea
/
MacZoop2.0
/
Required Classes
/
ZCommander.cpp
< prev
next >
Wrap
Text File
|
1999-02-12
|
8KB
|
357 lines
/*************************************************************************************************
*
*
* MacZoop - "the framework for the rest of us"
*
*
*
* ZCommander.cpp -- an object for handling commands
*
*
*
*
*
* © 1996, Graham Cox
*
*
*
*
*************************************************************************************************/
#include "ZCommander.h"
#include "MacZoop.h"
#include "ProjectSettings.h"
#if _ZCOMMANDER_DIALOG_AWARE
#include "ZDialog.h"
#endif
CLASSCONSTRUCTOR( ZCommander );
/*----------------------------------*** CONSTRUCTOR ***----------------------------------*/
ZCommander::ZCommander( ZCommander* aBoss )
: ZComrade()
{
classID = CLASS_ZCommander;
itsBoss = aBoss;
itsUnderlings = NULL;
if ( itsBoss )
itsBoss->AddUnderling( this );
}
ZCommander::ZCommander()
: ZComrade()
{
classID = CLASS_ZCommander;
itsBoss = NULL;
itsUnderlings = NULL;
}
/*-----------------------------------*** DESTRUCTOR ***----------------------------------*/
ZCommander::~ZCommander()
{
// kill any timers associated with this commander
KillAllTimers( this );
// dispose of any objects whose boss this is
if ( itsUnderlings )
itsUnderlings->DisposeAll();
// remove us from our boss
if ( itsBoss )
itsBoss->RemoveUnderling( this );
}
/*-------------------------------*** HANDLECOMMAND ***---------------------------------*/
/*
if it has a boss, it passes the command to it for handling
----------------------------------------------------------------------------------------*/
void ZCommander::HandleCommand( const short menuID, const short itemID )
{
if ( itsBoss )
itsBoss->HandleCommand( menuID, itemID );
}
/*-------------------------------*** HANDLECOMMAND ***--------------------------------*/
/*
pass command object to boss if there is one
----------------------------------------------------------------------------------------*/
void ZCommander::HandleCommand( const long theCommand )
{
// pass edit menu commands to the local handler methods. It is easier to override
// those methods than to override this method, but the choice is yours.
switch ( theCommand )
{
case kCmdCut:
DoCut();
break;
case kCmdCopy:
DoCopy();
break;
case kCmdPaste:
DoPaste();
break;
case kCmdClear:
DoClear();
break;
case kCmdSelectAll:
DoSelectAll();
break;
default:
if ( itsBoss )
itsBoss->HandleCommand( theCommand );
break;
}
}
/*-------------------------------*** UPDATEMENUS ***---------------------------------*/
/*
if it has a boss, it asks it to update its menus
----------------------------------------------------------------------------------------*/
void ZCommander::UpdateMenus()
{
// enable the paste command if there is something this object can
// paste.
if ( CanPasteType())
gMenuBar->EnableCommand( kCmdPaste );
if ( itsBoss )
itsBoss->UpdateMenus();
}
/*-----------------------------*** HANDLEAPPLEEVENT ***-------------------------------*/
/*
if it has a boss, it passes the command to it for handling. Override this to process
apple events you are interested in, call the inherited method for others.
----------------------------------------------------------------------------------------*/
void ZCommander::HandleAppleEvent( AEEventClass aeClass, AEEventID aeID,
AppleEvent* aeEvt, AppleEvent* reply )
{
if ( itsBoss )
itsBoss->HandleAppleEvent( aeClass, aeID, aeEvt, reply );
}
/*-----------------------------------*** IDLE ***-------------------------------------*/
/*
called periodically if this is in the chain of command. It passes the call up to its boss.
----------------------------------------------------------------------------------------*/
void ZCommander::Idle()
{
if ( itsBoss )
itsBoss->Idle();
}
/*-----------------------------------*** TYPE ***-------------------------------------*/
/*
this user is typing while this commander is active
----------------------------------------------------------------------------------------*/
void ZCommander::Type( const char theKey, const short modifiers )
{
if ( theKey == BACKSPACE_KEY )
DoClear();
else
{
if ( itsBoss )
itsBoss->Type( theKey, modifiers );
}
}
/*--------------------------------*** DOSUSPEND ***-----------------------------------*/
/*
the app is suspending
----------------------------------------------------------------------------------------*/
void ZCommander::DoSuspend()
{
if ( itsBoss )
itsBoss->DoSuspend();
}
/*---------------------------------*** DORESUME ***-----------------------------------*/
/*
the app is resuming
----------------------------------------------------------------------------------------*/
void ZCommander::DoResume()
{
if ( itsBoss )
itsBoss->DoResume();
}
/*-------------------------------*** ADDUNDERLING ***---------------------------------*/
/*
adds <anUnderling> to its list of underlings
----------------------------------------------------------------------------------------*/
void ZCommander::AddUnderling( ZCommander* anUnderling )
{
if ( itsUnderlings == NULL )
FailNIL( itsUnderlings = new ZCommanderList());
itsUnderlings->AppendItem( anUnderling );
}
/*-----------------------------*** REMOVEUNDERLING ***--------------------------------*/
/*
removes <anUnderling> from its list of underlings
----------------------------------------------------------------------------------------*/
void ZCommander::RemoveUnderling( ZCommander* anUnderling )
{
if ( itsUnderlings && itsUnderlings->Contains( anUnderling ))
{
itsUnderlings->DeleteObject( anUnderling );
if ( itsUnderlings->CountItems() < 1 )
ForgetObject( itsUnderlings );
}
}
/*--------------------------------*** SENDMESSAGE ***---------------------------------*/
/*
send the message to the boss as well as any nominated listeners
----------------------------------------------------------------------------------------*/
void ZCommander::SendMessage( long aMessage, void* msgData )
{
// we always send this message to our boss
if ( itsBoss )
itsBoss->ReceiveMessage( this, aMessage, msgData );
ZComrade::SendMessage( aMessage, msgData );
}
/*--------------------------------*** SENDMESSAGE ***---------------------------------*/
/*
send the message to the boss as well as any nominated listeners
----------------------------------------------------------------------------------------*/
void ZCommander::SendMessage( ZMessage* aMessage )
{
// we always send this message to our boss
if ( itsBoss )
itsBoss->ReceiveMessage( this, aMessage );
ZComrade::SendMessage( aMessage );
}
/*-------------------------------*** WRITETOSTREAM ***--------------------------------*/
/*
make a note of who our boss and underlings are
----------------------------------------------------------------------------------------*/
void ZCommander::WriteToStream( ZStream* aStream )
{
#if _MACZOOP_STREAMS
ZComrade::WriteToStream( aStream );
// write boss of this commander- it's OK if it's NULL.
aStream->WriteObject( itsBoss );
// we do not explicitly write the underlings to the stream, since that is set
// up anyway when a command chain is relinked from a stream.
#endif
}
/*-------------------------------*** READFROMSTREAM ***-------------------------------*/
/*
read in our boss and underlings from the stream
----------------------------------------------------------------------------------------*/
void ZCommander::ReadFromStream( ZStream* aStream )
{
#if _MACZOOP_STREAMS
ZComrade::ReadFromStream( aStream );
itsBoss = (ZCommander*) aStream->ReadObject();
// add this commander to its bosses underlings list:
if ( itsBoss )
itsBoss->AddUnderling( this );
#endif
}
/*-------------------------------*** OPENSUBDIALOG ***--------------------------------*/
/*
open a dialog with the given ID as an underling of this commander (convenience method)
----------------------------------------------------------------------------------------*/
ZDialog* ZCommander::OpenSubDialog( const short dlogID )
{
#if _ZCOMMANDER_DIALOG_AWARE
ZDialog* zd;
SetBeachBallCursor();
FailNIL( zd = new ZDialog( this, dlogID ));
try
{
zd->InitZWindow();
zd->Select();
}
catch( OSErr err )
{
ForgetObject( zd );
throw err;
}
return zd;
#else
return NULL;
#endif
}